﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Water.Web.Controls.BaseInterface;
using Water.Resources.Globalize;

namespace Water.Web.Controls
{
    [ToolboxData("<{0}:DataPager runat=server></{0}:DataPager>")]
    public class DataPager : System.Web.UI.WebControls.DataPager, ICallBackable, IUpdatedableControl,IQuery
    {
        #region private字段

        private bool isCallBack = true;
        private string displayDuringCallBackTextControls="";

        private bool isUpdateAfterCallBack = false;
        private UpdatedableControlRenderMode renderMode = UpdatedableControlRenderMode.Div;
        private string duringCallBackText = "";
        private string updateJS = "";
        private List<string> callBackControls = new List<string>();

        private int buttonCount = 10;
        private bool displayPageInfo = true;
        private TextAlign pageButtonAlign = TextAlign.Right;

        private string queryTarget;
        private List<string> querySource = new List<string>();

        #endregion

        [Browsable(true), GlobalizeCategory("Appearance"), GlobalizeDescription("PageButtonAlign"), DefaultValue(TextAlign.Right)]
        public TextAlign PageButtonAlign
        {
            get { return pageButtonAlign; }
            set { pageButtonAlign = value; }
        }

        [Browsable(true), GlobalizeCategory("Appearance"), GlobalizeDescription("DisplayPageInfo"), DefaultValue(true)]
        public bool DisplayPageInfo
        {
            get { return displayPageInfo; }
            set { displayPageInfo = value;}
        }

        [Browsable(true), GlobalizeCategory("Appearance"), GlobalizeDescription("ButtonCount"), DefaultValue(10)]
        public int ButtonCount
        {
            get { return buttonCount; }
            set { buttonCount = value; }
        }

        

        #region ICallBackable 成员

        [Browsable(true), GlobalizeCategory("AJAX"), GlobalizeDescription("IsCallBack"), DefaultValue(true)]
        public bool IsCallBack
        {
            get
            {
                return this.isCallBack;
            }
            set
            {
                this.isCallBack = value;
            }
        }
        [Browsable(true), GlobalizeCategory("AJAX"), GlobalizeDescription("CallBackTargets")]
        public string CallBackTargets
        {
            get
            {
                if (this.IsCallBack)
                {
                    return this.displayDuringCallBackTextControls;
                }
                return string.Empty;
            }
            set
            {
                this.displayDuringCallBackTextControls = value;
            }
        }

        #endregion

        #region IUpdatedableControl 成员
        [Browsable(true), GlobalizeCategory("AJAX"), GlobalizeDescription("AutoUpdatedAfterCallBack"), DefaultValue(false)]
        public bool AutoUpdatedAfterCallBack
        {
            get
            {
                return this.isUpdateAfterCallBack;
            }
            set
            {
                this.isUpdateAfterCallBack = value;
            }
        }
        [Browsable(false), DefaultValue(false)]
        public bool IsUpdatedAfterCallBack
        {
            get
            {
                return this.isUpdateAfterCallBack;
            }
            set
            {
                this.isUpdateAfterCallBack = value;
            }
        }

        [Browsable(true), GlobalizeCategory("AJAX"), GlobalizeDescription("DuringCallBackText")]
        public string DuringCallBackText
        {
            get
            {
                if (this.duringCallBackText != null)
                {
                    return this.duringCallBackText;
                }
                return string.Empty;
            }
            set
            {
                this.duringCallBackText = value;
            }
        }
        [Browsable(true), GlobalizeCategory("AJAX"), GlobalizeDescription("RenderMode"), DefaultValue(UpdatedableControlRenderMode.Span)]
        public UpdatedableControlRenderMode RenderMode
        {
            get
            {
                return this.renderMode;
            }
            set
            {
                this.renderMode = value;
            }
        }

        [Browsable(false), GlobalizeCategory("AJAX"), GlobalizeDescription("UpdateJS")]
        public virtual string UpdateJS
        {
            get
            {
                return this.updateJS;
            }
            set
            {
                this.updateJS = value;
            }
        }

        [Browsable(false)]
        public List<string> CallBackControls
        {
            get { return callBackControls; }
            set { callBackControls = value; }
        }
        #endregion

        #region IQuery 成员

        [Browsable(true), GlobalizeCategory("QUERY"), GlobalizeDescription("QueryTarget")]
        public string QueryTarget
        {
            get
            {
                return this.queryTarget;
            }
            set
            {
                this.queryTarget = value;
            }
        }

        [Browsable(false)]
        public List<string> QuerySource
        {
            get
            {
                return this.querySource;
            }
            set
            {
                this.querySource = value;
            }
        }

        #endregion

        #region override
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            PageManager.Register(this);
        }
        protected override void Render(HtmlTextWriter writer)
        {
            if (this is IUpdatedableControl)
                Marker.WriteAJAXBegin(writer, this);
            base.Render(writer);
            if (this is IUpdatedableControl)
                Marker.WriteAJAXEnd(writer, this);
        }

        public override string PagedControlID
        {
            get
            {
                if (!string.IsNullOrEmpty(base.PagedControlID))
                    return Water.Web.Controls.Util.Utils.FindControlEx(base.PagedControlID, this.Page).UniqueID;
                return base.PagedControlID;
            }
            set
            {
                base.PagedControlID = value;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            if (this.Fields.Count == 0)
            {
                NextPreviousPagerField first_previous = new NextPreviousPagerField();
                first_previous.ShowFirstPageButton = true;
                first_previous.ShowPreviousPageButton = true;
                first_previous.ShowNextPageButton = false;
                first_previous.ShowLastPageButton = false;
                first_previous.FirstPageText = MessageResource.PagerFirst;
                first_previous.PreviousPageText = MessageResource.PagerPrevious;
                first_previous.NextPageText = MessageResource.PagerNext;
                first_previous.LastPageText = MessageResource.PagerLast;

                NumericPagerField num = new NumericPagerField();
                num.ButtonCount = this.ButtonCount;

                NextPreviousPagerField last_next = new NextPreviousPagerField();
                last_next.ShowFirstPageButton = false;
                last_next.ShowPreviousPageButton = false;
                last_next.ShowNextPageButton = true;
                last_next.ShowLastPageButton = true;
                last_next.FirstPageText = MessageResource.PagerFirst;
                last_next.PreviousPageText = MessageResource.PagerPrevious;
                last_next.NextPageText = MessageResource.PagerNext;
                last_next.LastPageText = MessageResource.PagerLast;

                if (DisplayPageInfo)
                {
                    Water.Web.Controls.TemplatePagerField template = new Water.Web.Controls.TemplatePagerField();
                    this.Fields.Add(template);
                }
                this.Fields.Add(first_previous);
                this.Fields.Add(num);
                this.Fields.Add(last_next);
            }
            base.OnInit(e);
        }
        protected override void RenderChildren(HtmlTextWriter writer)
        {
            //base.RenderChildren(writer);
            writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
            writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
            writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
            writer.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);//table
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);//tr

            writer.AddAttribute(HtmlTextWriterAttribute.Align, "left");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);//td


            if (TextAlign.Right == pageButtonAlign)
            {
                foreach (Control _ctl in this.Controls)
                {
                    if (_ctl is DataPagerFieldItem)
                    {
                        DataPagerFieldItem _dpfi = _ctl as DataPagerFieldItem;
                        if (_dpfi.PagerField is System.Web.UI.WebControls.TemplatePagerField)
                            _ctl.RenderControl(writer);
                    }
                }
            }
            else
            {
                foreach (Control _ctl in this.Controls)
                {
                    if (_ctl is DataPagerFieldItem)
                    {
                        DataPagerFieldItem _dpfi = _ctl as DataPagerFieldItem;
                        if (!(_dpfi.PagerField is System.Web.UI.WebControls.TemplatePagerField))
                            _ctl.RenderControl(writer);
                    }
                    else
                        _ctl.RenderControl(writer);
                }
            }

            writer.RenderEndTag();//td

            writer.AddAttribute(HtmlTextWriterAttribute.Align, "right");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);//td

            if (TextAlign.Right == pageButtonAlign)
            {
                foreach (Control _ctl in this.Controls)
                {
                    if (_ctl is DataPagerFieldItem)
                    {
                        DataPagerFieldItem _dpfi = _ctl as DataPagerFieldItem;
                        if (!(_dpfi.PagerField is System.Web.UI.WebControls.TemplatePagerField))
                            _ctl.RenderControl(writer);
                    }
                    else
                        _ctl.RenderControl(writer);
                }
            }
            else
            {

                foreach (Control _ctl in this.Controls)
                {
                    if (_ctl is DataPagerFieldItem)
                    {
                        DataPagerFieldItem _dpfi = _ctl as DataPagerFieldItem;
                        if (_dpfi.PagerField is System.Web.UI.WebControls.TemplatePagerField)
                            _ctl.RenderControl(writer);
                    }
                }
            }
            
            writer.RenderEndTag();//td

            writer.RenderEndTag();//tr
            writer.RenderEndTag();//table
        }
        #endregion

       
    }

    internal class TemplatePagerField : System.Web.UI.WebControls.TemplatePagerField
    {
        public TemplatePagerField()
        {
            if (null == this.PagerTemplate)
                this.PagerTemplate = new DefaultPagerTemplate();
        }
    }
    internal class DefaultPagerTemplate : ITemplate
    {

        #region ITemplate 成员

        public void InstantiateIn(Control container)
        {
            DataPagerFieldItem dataPagerFieldItem = container as DataPagerFieldItem;
            System.Web.UI.WebControls.Label label = new System.Web.UI.WebControls.Label();
            System.Web.UI.WebControls.DataPager pager=dataPagerFieldItem.Pager;
            int totalRowCount = pager.TotalRowCount;
            int pageSize=pager.PageSize;
            int pageCount=(int)Math.Ceiling((double)totalRowCount / (double)pageSize);
            int startRowIndex = pager.StartRowIndex+1;
            int endRowIndex=(pager.StartRowIndex+pager.PageSize) > totalRowCount ? totalRowCount:(pager.StartRowIndex+pager.PageSize);
            int currentPage=(startRowIndex+pageSize) / pageSize;
            label.Text = string.Format(MessageResource.PagerInfo, totalRowCount, currentPage, pageCount, startRowIndex, endRowIndex);
            container.Controls.Add(label);
        }

        #endregion
    }
}
